<template>
  <el-dialog
    title="祝福语修改通知"
    :visible.sync="visible"
    width="1200px">
      <XmTable :config="tableConfig" :noWatchParams="{
        deptId: cityId
      }" class="table" ref="table">
        <div slot="filter" class="mb-10">
          <span class="mr-10 c-999">已选{{getSelection().length}}笔订单</span>
          <el-button type="primary" size="small" @click="onBatchPrint">批量打印</el-button>
          <el-button type="primary" size="small" @click="fetch">刷新</el-button>
        </div>
        <template slot="column-productionState" slot-scope="{data}">
          <el-tag size="small" type="primary" v-if="data.productionState">
            {{cakeLabelMap[data.productionState]}}
          </el-tag>
        </template>
        <template slot="column-operate" slot-scope="{data}">
          <el-button type="primary" class="edit-btn" size="mini" @click="print(data)">
            打印
          </el-button>
        </template>
      </XmTable>

      <!-- 批量打印 -->
      <div style="position: fixed;top: -100%;right: -100%;height: 350px;">
        <printForm ref="printForm" id="printForm" :needBlessings="needBlessings" :isOpenPrintPreview="isOpenPrintPreview"></printForm>
      </div>
  </el-dialog>
</template>

<script>
import {tableConfig} from './config'
import {cakeLabelMap} from '@/const'
import printForm from "@/views/scheduling/index/cake/components/printForm";
import {updateBlessingNotice, getNoticePage} from '@/api/createOrder'
export default {
  props: {
    cityId: {
      type: [String,Number]
    },
    needBlessings: {
      type: Boolean,
    },
    isOpenPrintPreview: {
      type: Boolean,
    }
  },
  components: {
    printForm,
  },
  data() {
    return {
      visible: false,
      tableConfig,
      cakeLabelMap,
      StampStorageReturnList: []
    }
  },
  methods: {
    show() {
      this.visible = true
      this.fetch()
    },
    onBatchPrint() {
      const checkeds = this.getSelection()
      
      if (checkeds && checkeds.length) {
        // if (checkeds.some(item => item.orderStatus != 3 && item.orderStatus != 4)) {
        //   this.$message({
        //     type: 'warning',
        //     message: '打印失败，只能打印生产中和已入库的蛋糕'
        //   })
        //   return
        // }
        this.$confirm('请确认是否打印所选订单！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(() => {
          this.$refs.printForm.callPrint(checkeds.map(item => {
            return {
              ...item,
              id: item.orderDetailId
            }
          }), 1000).then(res => {
            if (res) {
              this.updateBlessingNotice({ids: checkeds.map(item => item.id)})
              this.fetch()
            }
          })
        })
      } else {
        this.$message({
          type: 'warning',
          message: '请先勾选记录！'
        })
      }
    },
    print(data) {
      this.$refs.printForm.callPrint([{
        ...data,
        id: data.orderDetailId
      }], 1000).then(() => {
        this.updateBlessingNotice({ids: [data.id]})
        this.fetch()
      })
    },
    getSelection() {
      const tables = this.$refs.table && this.$refs.table.getSelection()
      return tables || []
    },
    
    setNoticeTimer() {
      this.noticeTimer = setInterval(() => {
        if (!this.visible) {
          const params = {
            page: 1,
            size: 10,
            deptId: this.cityId
          }
          getNoticePage(params).then(res => {
            if (res.data.data.rows && res.data.data.rows.length) {
              this.visible = true
              this.fetch()
            }
          })
        }
      }, 10 * 1000)
    },
    clearTimer() {
      this.noticeTimer && clearInterval(this.noticeTimer)
    },
    fetch() {
      this.$refs.table && this.$refs.table.fetch()
    },
    tableFetch() {
      return this.$refs.table.requestFetch()
    },
    updateBlessingNotice(params) {
      updateBlessingNotice(params)
    },
    loading(isLoading) {
      if (isLoading) {
        this.loadingInstance = this.$loading({
          lock: true,
          text: '请求打印中，请稍等...',
        });
      } else {
        this.loadingInstance && this.loadingInstance.close()
      }
    },
  },
  created() {
    this.setNoticeTimer()
  },
  beforeDestroy() {
    this.clearTimer()
  }
}
</script>

<style lang="scss" scoped>
</style>
